package com.yun.zf.admin.controller;

import cn.hutool.core.codec.Base64;
import com.cxytiandi.encrypt.springboot.annotation.Decrypt;
import com.cxytiandi.encrypt.springboot.annotation.Encrypt;
import com.google.code.kaptcha.Producer;
import com.yun.zf.common.admin.model.search.CommunitySearchMo;
import com.yun.zf.common.constant.Constants;
import com.yun.zf.common.utils.IdUtils;
import com.yun.zf.common.vo.Response;
import com.yun.zf.system.exception.ExceptionUtil;
import com.yun.zf.system.redis.RedissonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.io.FastByteArrayOutputStream;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 验证码操作处理
 * 
 * @author ruoyi
 */
@Api(tags = "验证码操作处理")
@RestController
@Slf4j
public class CaptchaController
{
    @Autowired
    private RedissonUtils redissonUtils;
    @Resource(name = "captchaProducer")
    private Producer captchaProducer;

    @Resource(name = "captchaProducerMath")
    private Producer captchaProducerMath;
    @Value("${ruoyi.captchaType}")
    private String captchaType;
    /**
     * 生成验证码
     */
    @ApiOperation(value ="生成验证码")
    @GetMapping("/captchaImage")
    public Response getCode(HttpServletResponse response)
    {
        try
        {
        // 保存验证码信息
        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;

        String capStr = null, code = null;
        BufferedImage image = null;
        log.info("验证码类型{}",captchaType);
        // 生成验证码
        if ("math".equals(captchaType))
        {
            String capText = captchaProducerMath.createText();
            log.info("capText-------{}",capText);
            capStr = capText.substring(0, capText.lastIndexOf("@"));
            code = capText.substring(capText.lastIndexOf("@") + 1);
            image = captchaProducerMath.createImage(capStr);
        }
        else if ("char".equals(captchaType))
        {
            capStr = code = captchaProducer.createText();
            image = captchaProducer.createImage(capStr);
        }

        redissonUtils.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        log.info("我走到这一步了吗？？？？？？？");
        // 转换流信息写出
        FastByteArrayOutputStream os = new FastByteArrayOutputStream();

            ImageIO.write(image, "jpg", os);
            log.info("我走到这一步了--------");
            Map<String,Object> map =new HashMap();
            map.put("uuid", uuid);
            map.put("img", Base64.encode(os.toByteArray()));

            return Response.success(map);
        }
        catch (Exception e)
        {
            log.info("异常错误信息{}",ExceptionUtil.getExceptionMessage(e));
            return Response.error(e.getMessage());
        }

    }

    @ApiOperation(value = "生成加密字符串")
    @PostMapping("/getEncryptStr")
   @Encrypt
    public String getEncryptStr(String context){
        return  context;
    }


    @ApiOperation(value = "加密字符串解密")
    @GetMapping("/getDecrypt")
    @Decrypt(decyptParam = "context")
    public String getDecrypt(String context) {
     return  context;
    }

    @Encrypt
    @GetMapping("/encryptEntity")
    @ApiOperation(value = "对象加解密")
    public CommunitySearchMo encryptEntity() {
        CommunitySearchMo dto = new CommunitySearchMo();
        dto.setContext("金地自在城");
        dto.setPageSize(10);
        dto.setCommunityType("S");
        dto.setAveragePrice("");
        return dto;
    }
    @Encrypt
    @Decrypt
    @PostMapping("/save")
    @ApiOperation(value = "对象加解密111")
    public CommunitySearchMo save(@RequestBody CommunitySearchMo dto) {

        return dto;
    }

}
